<template>
  <div class="chart-container">
    <div id="water-supply-statistics-chart" :style="getAvailableHeight"></div>
  </div>
</template>

<script>
import * as echarts from 'echarts';

require('echarts/theme/macarons') // echarts theme

export default {
  name: 'WaterSupplyStatistics',
  props: {
    addHeight: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      screenHeight: null,
      waterSupplyStatisticsChart: undefined,
      baseOption: {
        legend: {
          data: ['供水量 m³', '环比 %']
        },
        grid: {
          top: '5%',
          right: '4%',
          bottom: '5%',
          left: '5%',
          containsLabel: true
        },
        tooltip:{
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          }
        },
        xAxis: {
          type: 'category',
          data: [],
        },
        yAxis: [
          {
            type: 'value',
            axisLine: {
              show: true,
            },
            axisLabel: {
              formatter: '{value} m³'
            }
          },
          {
            type: 'value',
            position: 'right',
            axisLine: {
              show: true,
            },
            axisLabel: {
              formatter: '{value} %'
            }
          }
        ],
        series: [
          {
            name: '供水量 m³',
            data: [],
            type: 'bar',
          },
          {
            name: '环比 %',
            data: [],
            type: 'line',
            smooth: true,
            yAxisIndex: 1,
          }
        ]
      },
    }
  },
  methods: {
    initChart() {
      this.waterSupplyStatisticsChart = echarts.init(document.getElementById('water-supply-statistics-chart'), 'macarons')
      this.waterSupplyStatisticsChart.setOption(this.baseOption)
    },
    resizeCharts(height) {
      this.screenHeight = height
      this.$nextTick(() => {
        this.waterSupplyStatisticsChart.resize()
      })
    },
    updateOption(data) {
      const option = Object.assign({}, this.baseOption)
      option.xAxis.data = data.timeList
      option.series[0].data = data.supply
      option.series[1].data = data.hb

      this.waterSupplyStatisticsChart.setOption(option)
    }
  },
  computed: {
    getAvailableHeight() {
      const h = this.screenHeight - 528 + this.addHeight
      return {width: '100%', height: h + 'px'}
    }
  },
  created() {
    this.screenHeight = document.body.clientHeight
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  }
}

</script>

<style scoped lang="scss">
.chart-container {
  margin-top: 10px;
}
</style>
